# -*- coding: utf-8 -*-
"""
Created on Sat Nov 20 18:37:12 2021

@author: Administrator
"""

# -*- coding: utf-8 -*-
"""
Created on Sat Nov 20 12:44:15 2021

@author: Administrator
"""

#引入：
import pygame as pg
import random
from pygame.color import THECOLORS
import numpy as np

brown_images = ['./pic/brown_horse_stand.png','./pic/brown_horse_run.png']
reverse_brown_images = ['./pic/brown_horse_reverse.png','./pic/brown_horse_reverse2.png']

#创建个类：
class BrownHorse(pg.sprite.Sprite):
    #初始化对象
    def __init__(self,location,speed,images,*groups):
        pg.sprite.Sprite.__init__(self)
        self.frame = 0  # 角色初始帧
        self.image = pg.image.load(images[self.frame])
        #得到定义图像边界的矩形
        self.rect = self.image.get_rect()
        #小鸟的初始位置
        self.rect.left,self.rect.top = location
        self.radius = 300 - location[1] - 0 # 绕圈的半径要减去角色的大小
        #创建速度属性
        self.speed = speed
        self.timer = 0 # 设置计时器.
        self.theta_right = -np.pi/2
        self.theta_left = -np.pi/2

    #增加运动方法
    def move(self):
        self.rect = self.rect.move(self.speed)
        #检测是否达到窗口边界
        if self.rect.left < 0 or self.rect.right > width:
            self.speed[0] = -self.speed[0]
        if self.rect.top < 0 or self.rect.bottom > height:
            self.speed[1] = -self.speed[1]


    def update(self, dt, images1,images2):
        self.timer += dt # Increment the timer.
        # 如果计时器大于期望值
        if self.timer >= .2:
            self.timer = 0 # Reset the timer.
            self.frame += 1 # Increment the frame index.
            self.frame %= len(images1) # 保持frame在图片数量的范围之内循环.
            if self.rect.top <= 300:
                self.image = pg.image.load(images1[self.frame]) # Switch the image.
            else:
                self.image = pg.image.load(images2[self.frame]) # 当马跑到下半圈时反转，变成马头朝左的图片.
            if 300 <=self.rect.left < 700 and self.rect.top < 300: # 当马在上面直道时，向右走直线。
                self.rect.left += self.speed[0]
            elif self.rect.left >= 700:  # 当马在右侧弯道时沿圆弧跑动。
                self.rect.left,self.rect.top = self.calc_y_coordinate_right_cicle(self.theta_right)
            elif 300 < self.rect.left < 700 and self.rect.top > 300: # 当马在下面直道时，向左直走。
                self.rect.left -= self.speed[0]
            elif self.rect.left <= 300 :  # 当马在左侧弯道时，沿圆弧跑动。
                self.rect.left,self.rect.top = self.calc_y_coordinate_left_cicle(self.theta_left)

    # 增加角色左边绕圈方法
    def calc_y_coordinate_left_cicle(self,theta):
        a, b = (300., 300.)
        r = self.radius
        if self.theta_left <= np.pi/2:
            self.theta_left += 0.1
            x = a - r * np.cos(theta)
            y = b - r * np.sin(theta)
            return (x,y)
        else:
            self.theta_left = -np.pi/2
            x = a - r * np.cos(theta)
            y = b - r * np.sin(theta)
            return (x,y)

    # 增加角色右边绕圈方法
    def calc_y_coordinate_right_cicle(self,theta):
        a, b = (600, 250)
        r = self.radius
        if self.theta_right <= np.pi/2:
            self.theta_right += 0.1
            x = a + r * np.cos(-theta)
            y = b - r * np.sin(-theta)
            return (x,y)
        else:
            self.theta_right = -np.pi/2
            x = a + r * np.cos(-theta)
            y = b - r * np.sin(-theta)
            return (x,y)


#主循环
def main():
    #设置窗口大小、颜色、载入图片
    size = width,height = 1000,600
    screen = pg.display.set_mode(size)
    screen.fill(THECOLORS['white'])
    clock = pg.time.Clock()
    # 角色位置

    horses = []
    for num in range(0,6): # 生成6匹马
        # 角色位置
        location = [300,num*20]   # 角色位置在x坐标300，y坐标间隔20依次递增
        #速度随机
        speed = [5 + random.randint(5,10),0]  # 随机设置马的初始速度。为方向向右， 值为 5+x
        # 使用pygame的精灵组，生成一匹马，因为要实现奔跑动作所以必须使用精灵组进行造型切换
        horse = pg.sprite.Group(BrownHorse(location,speed,brown_images))
        horses.append(horse)
    dt = 0

    mRunning = True
    while mRunning:
        for event in pg.event.get():
            if event.type == pg.QUIT:
                mRunning = False
        #时间延迟
        pg.time.delay(20)
        screen.fill(THECOLORS['white']) # 屏幕背景

        # 画比赛场地
        pg.draw.circle(screen,THECOLORS['red'],(300,300),300,0) # 左侧红色外圆
        pg.draw.circle(screen,THECOLORS['green'],(300,300),150,0) # 左侧绿色内圆
        pg.draw.circle(screen,THECOLORS['red'],(700,300),300,0)   # 右侧红色外圆
        pg.draw.circle(screen,THECOLORS['green'],(700,300),150,0) # 右侧绿色内圆
        pg.draw.rect(screen, THECOLORS['red'],[300,0,400,150],0)  # 上方红色方块
        pg.draw.rect(screen, THECOLORS['green'],[300,150,400,300],0) # 中间绿色方块
        pg.draw.rect(screen, THECOLORS['red'],[300,450,400,150],0)  # 下方红色方块

        for horse in horses:
            horse.update(dt,brown_images,reverse_brown_images)  # 循环
            horse.draw(screen)
        pg.display.flip()
        dt = clock.tick(30)/1000  # 设置时钟间隔为30毫秒
    pg.quit()


if __name__ == '__main__':
    pg.init()
    main()
    pg.quit()